package pl.enigmatic.reflection;

import java.lang.reflect.Method;

import pl.enigmatic.util.Reflection;


public class SimpleSetter implements Setter {

	private final Class<?> type;
	private final Method method;
	private final PropertyPath path;

	public SimpleSetter(final Class<?> cls, final String property, Class<?> type) throws SecurityException, NoSuchMethodException {
		this.path = new PropertyPath(property);
		this.type = type;
		method = cls.getMethod(Reflection.setter(property), new Class<?>[] { type });
	}

	@Override
	public boolean set(final Object target, final Object value) {
		try {
			method.invoke(target, value);
		} catch (final Exception e) {
			return false;
		}
		return true;
	}

	@Override
	public Class<?> getType() {
		return type;
	}

	@Override
	public PropertyPath getPath() {
		return path;
	}

}
